package xiaolong.lian.web;

import com.ucpaas.restDemo.SysConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import xiaolong.lian.exception.CustomException;
import xiaolong.lian.service.UserService;
import xiaolong.lian.util.Args;
import xiaolong.lian.util.Cookies;
import xiaolong.lian.util.Session;
import xiaolong.lian.util.Auth;

@Controller
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 发送验证码
     * @param tel
     * @return {msg:'message'}
     */
    @RequestMapping(value = "/sendcode", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String sendAuthCode(String tel) throws CustomException {
        new Args().ensureNotEmpty("手机号码", tel);
        String code = userService.sendAuthCode(tel);
        Session.set("code", code, Integer.parseInt(SysConfig.getInstance().getProperty("valid_minute")));
        Session.set("tel", tel, Integer.parseInt(SysConfig.getInstance().getProperty("valid_minute")));
        return "{\"msg\" : \"发送成功\"}";
    }

    /**
     * 注册
     * @param tel
     * @param password
     * @param code
     * @return {msg: 'message'}
     */
    @RequestMapping(value = "/register", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String register(String tel, String password, String code) throws CustomException {
        new Args().ensureNotEmpty("手机号码", tel).ensureNotEmpty("密码", password).ensureNotEmpty("验证码", code);
        String sent = Session.get("code");
        if(sent == null)
            throw new CustomException("验证码已过期");
        if(!tel.equals(Session.get("tel")))
            throw new CustomException("手机号已更改，请重新获取验证码");
        String token = userService.register(tel, password, sent, code);
        Cookies.addOne("tel", tel);
        Cookies.addOne("token", token, Auth.validDays * 24*60*60);
        return "{\"msg\" : \"注册成功，已自动登录\"}";
    }

    /**
     * 登录
     * @param tel
     * @param password
     * @return {msg: '登录成功'}
     */
    @RequestMapping(value = "/login", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String login(String tel, String password) throws CustomException {
        new Args().ensureNotEmpty("手机号码", tel).ensureNotEmpty("密码", password);
        String token = userService.login(tel, password);
        Cookies.addOne("tel", tel);
        Cookies.addOne("token", token, Auth.validDays * 24*60*60);
        return "{\"msg\" : \"登录成功\"}";
    }

    /**
     * 重置密码
     * @param tel
     * @param newpsw
     * @param code
     * @return {msg: '重置密码成功'}
     */
    @RequestMapping(value = "/resetpsw", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public String resetPassword(String tel, String newpsw, String code) throws CustomException {
        new Args().ensureNotEmpty("手机号码", tel).ensureNotEmpty("新密码", newpsw).ensureNotEmpty("验证码", code);
        String sent = Session.get("code");
        if(sent == null)
            throw new CustomException("验证码已过期");
        if(!tel.equals(Session.get("tel")))
            throw new CustomException("手机号已更改，请重新获取验证码");
        String token = userService.resetPassword(tel, newpsw, sent, code);
        Cookies.addOne("tel", tel);
        Cookies.addOne("token", token, Auth.validDays * 24*60*60);
        return "{\"msg\" : \"重置密码成功\"}";
    }
}
